<template>
  <div class="index">
    <!-- 频道列表 -->
    <van-tabs v-model="active">
      <!-- 频道与文章是一一对应的每个频道都需要有一个文章列表组件 -->
      <!-- article-list写在v-for中，每次循环都会生成一个文章列表组件 -->
      <!-- van-tab具有类似于懒加载的效果，只有激活了当前的tab才会去创建文章列表组件 -->
      <van-tab
        v-for="channel in channels"
        :key="channel.id"
        :title="channel.name"
      >
        <!-- <div class="scroll-wrapper"> -->
        <!-- {{ channel.name }} -->
        <!-- <p v-for="idx in 20" :key="idx">第{{ idx }}篇文章</p> -->
        <article-list :channel="channel"></article-list>
        <!-- </div> -->
      </van-tab>
      <!-- <van-tab title="标签 2">内容 2</van-tab>
      <van-tab title="标签 3">内容 3</van-tab>
      <van-tab title="标签 4">内容 4</van-tab>
      <van-tab title="标签 5">内容 5</van-tab>
      <van-tab title="标签 6">内容 6</van-tab>
      <van-tab title="标签 7">内容 7</van-tab>
      <van-tab title="标签 8">内容 8</van-tab> -->
    </van-tabs>
    <!-- 按钮s
      点击之后，弹层就会升起来
    -->
    <div class="bar-btn" @click="isShowChannelEdit = true">
      <van-icon name="wap-nav" />
    </div>
    <!-- 弹层  https://vant-contrib.gitee.io/vant/#/zh-CN/action-sheet -->
    <van-action-sheet v-model="isShowChannelEdit" title="标题">
      <ChannelEdit
        :channels="channels"
        @change-channel="hChangeChannel"
        @fixed-active-index="hFixedActiveIndex"
        :curIndex="active"
      />
    </van-action-sheet>
  </div>
</template>

<script>
import ChannelEdit from './channelEdit.vue'
// 引入这个组件
import ArticleList from './articleList.vue'
// import {dislike,report} from '../../api/article'
import { getChannels } from '../../api/channel'
export default {
  name: 'home',
  data () {
    return {
      isShowChannelEdit: false, // 是否显示频道编辑弹层
      active: 0,
      channels: [] // 频道列表
    }
  },
  // 注册
  components: {
    ChannelEdit,
    ArticleList
  },
  created () {
    this.loadChannels()
  },
  methods: {
    hFixedActiveIndex () {
      this.active--
    },
    async loadChannels () {
      try {
        const res = await getChannels()
        console.log(res)
        this.channels = res.data.data.channels
      } catch (err) {
        console.log(err)
      }
    },
    hChangeChannel (curIndex) {
      console.log('父组件收到了子组件中我的频道被点击', curIndex)
      // 切换频道
      this.active = curIndex
      // 关闭弹层
      this.isShowChannelEdit = false
    }
  }

}
</script>

<style lang="less" scoped >
.bar-btn {
  position: fixed;
  right: 5px;
  top: 57px;
  display: flex;
  align-items: center;
  background-color: #fff;
  opacity: 0.8;
  z-index: 1;
  .van-icon-wap-nav {
    font-size: 20px;
  }
}
</style>
